cd "Wilfahrt_APSR Replication Data"

*******************************************************************
**************** 25 DEGREE GRIDS *********************************
*******************************************************************

*Acled data
{
import delimited "Appendix/Larger Grids/AcledID_25degrees.csv", varnames(1) clear 
keep acled_id grid_id
save "Appendix/Larger Grids/Base_DVs1_25degrees.dta", replace

use "Appendix/Larger Grids/Base_DVs1_25degrees.dta", clear
sort acled_id
merge acled_id using "Data/Calculated Output/Acled_Merge.dta"

gen Battles = 0
replace Battles = 1 if event_type == "Battles"
gen Riots = 0
replace Riots = 1 if event_type == "Riots"
gen Civ_Violence = 0
replace Civ_Violence = 1 if event_type == "Violence against civilians"

gen Battles_pre2013 = 0
replace Battles_pre2013 = 1 if event_type == "Battles" & year < 2014
gen Remote_pre2013 = 0
replace Remote_pre2013 = 1 if event_type == "Explosions/Remote violence" & year < 2014

gen State_Actor = 0
replace State_Actor = 1 if interaction == 12 | interaction == 13 | interaction == 14 | interaction == 23 | interaction == 33 | interaction == 34 

replace Battles_pre2013 = 0 if State_Actor == 0  & year < 2014
replace Remote_pre2013 = 0 if State_Actor == 0  & year < 2014

gen ViolenceSum = Battles_pre2013+Remote_pre2013

bysort grid_id year: gen N_yrs_conflict_Acled_pre2013 = 1 if ViolenceSum > 0 
bysort grid_id year N_yrs_conflict_Acled_pre2013: gen N2 = _n 
bysort grid_id year N_yrs_conflict_Acled_pre2013: egen maxN2 = max(N2)
replace N_yrs_conflict_Acled_pre2013 = 0 if N2 > 1

collapse (rawsum) N_yrs_conflict_Acled_pre2013 Battles_pre2013 Remote_pre2013 Civ_Violence Battles Riots, by(grid_id)

gen Agg_Acled = Riots+Battles+Civ_Violence
gen ln_Agg_Acled = log(1+Agg_Acled)

gen Agg_Acled_Violence = Battles+Civ_Violence
gen ln_Agg_Acled_Violence = log(1+Agg_Acled_Violence)

replace Battles_pre2013 = 0 if Battles_pre2013 == .
replace Remote_pre2013 = 0 if Remote_pre2013 == .
replace N_yrs_conflict_Acled_pre2013 = 0 if N_yrs_conflict_Acled_pre2013 == .

gen Agg_Acled_pre2013 = Battles_pre2013+Remote_pre2013
gen ln_Agg_Acled_pre2013 = log(1+Agg_Acled_pre2013)
gen ln_N_yrs_conflict_Acled_pre2013 = log(1+N_yrs_conflict_Acled_pre2013)

save "Appendix/Larger Grids/Acled_GRID_Final_25degrees.dta", replace
}

* Import, clean, match Meta Wealth Data
{
import delimited "Appendix/Larger Grids/Meta_Wealth_25degrees.csv", varnames(1) clear 
collapse (mean) rwi, by(grid_id)
sort grid_id
save "Appendix/Larger Grids/MetaWealth_25km_Final.dta", replace
}

* Import, clean, match GED data
{
import delimited "Appendix/Larger Grids/GEDID_25degrees.csv", varnames(1) clear 
keep ged_id grid_id
save "Appendix/Larger Grids/Base_DVs2_25degrees.dta", replace

use "Appendix/Larger Grids/Base_DVs2_25degrees.dta", clear
sort ged_id
merge ged_id using "Data/Calculated Output/GED_Merge.dta"
drop if _merge == 2

collapse (first) year active_year type_of_violence conflict_new_id where_prec N_Battles_State N_Battles_NonState N_Battles_Total N_Battles_State_pre2013 N_Battles_NonState_pre2013 N_Battles_Total_pre2013, by (ged_id grid_id)

bysort grid_id conflict_new_id: gen N_Conflicts = 1
bysort grid_id conflict_new_id: gen N = _n
replace N_Conflicts = 0 if N > 1

bysort grid_id conflict_new_id: gen N_Conflicts_pre2013 = 1 if year < 2014
bysort grid_id conflict_new_id: gen N2 = _n if year < 2014
replace N_Conflicts_pre2013 = 0 if N2 > 1 & year < 2014

bysort grid_id year: gen N_yrs_conflict = 1 if active_year == 1
bysort grid_id year: gen N3= _n
bysort grid_id year: replace N_yrs_conflict = 0 if N3 > 1

bysort grid_id year: gen N_yrs_conflict_pre2013 = 1 if active_year == 1 
bysort grid_id year: gen N4 = _n 
replace N_yrs_conflict_pre2013 = 0 if N4 > 1 
replace N_yrs_conflict_pre2013 = . if  year > 2013

bysort grid_id conflict_new_id: gen N_StateConflicts = 1 if active_year == 1 & type_of_violence == 1
bysort grid_id conflict_new_id: gen N5 = _n
replace N_StateConflicts = 0 if N5 > 1

bysort grid_id conflict_new_id: gen N_StateConflicts_pre2013 = 1 if active_year == 1 & type_of_violence == 1 
bysort grid_id conflict_new_id: gen N6 = _n 
replace N_StateConflicts_pre2013 = 0 if N6 > 1 

bysort grid_id year: gen N_yrs_Stateconflict = 1 if active_year == 1 & type_of_violence == 1
bysort grid_id year: gen N7 = _n
replace N_yrs_Stateconflict = 0 if N7 > 1

bysort grid_id year: gen N_yrs_Stateconflict_pre2013  = 1 if active_year == 1 & type_of_violence == 1 
bysort grid_id year: gen N8 = _n 
replace N_yrs_Stateconflict_pre2013  = 0 if N8 > 1 
replace N_yrs_Stateconflict_pre2013 = . if  year > 2013

collapse (rawsum) N_yrs_Stateconflict_pre2013 N_Conflicts_pre2013 N_StateConflicts_pre2013 N_yrs_conflict_pre2013 N_Battles_Total_pre2013 N_Battles_State_pre2013 N_Battles_NonState_pre2013  N_Conflicts N_yrs_conflict N_Battles_Total N_Battles_State N_Battles_NonState, by(grid_id)

gen ln_N_yrs_conflict_pre2013 = log(1+N_yrs_conflict_pre2013)
gen ln_N_Battles_Total_pre2013 = log(1+N_Battles_Total_pre2013)
gen ln_N_Conflicts_pre2013 = log(1+N_Conflicts_pre2013)
gen ln_N_Battles_State_pre2013 = log(1+N_Battles_State_pre2013)
gen ln_N_StateConflicts_pre2013 = log(1+N_StateConflicts_pre2013)
gen ln_N_yrs_Stateconflict_pre2013 = log(1+N_yrs_Stateconflict_pre2013)

save "Appendix/Larger Grids/GED_GRID_Final_25degrees.dta", replace
}

*Controls
{
import delimited "Appendix/Larger Grids/White_Eco_25.csv", varnames(1) clear 
drop id
sort grid_id
save "Appendix/Larger Grids/White_Eco_25.dta", replace

import delimited "Appendix/Larger Grids/GEDID_25degrees.csv", varnames(1) clear 

rename centroids1 grid_longitude
rename centroids2 grid_latitude
rename grid_perch20x grid_perc_h20
rename grid_area_kmxx grid_area_km
rename water_area_km2x water_area_km2
rename grid_idx grid_id
rename countryx country
rename country_area_kmx country_area_km
rename nightlight_20132_meany nightlight_20132_mean

replace water_area_km2 = 0 if water_area_km2 == .
replace grid_perc_h20 = 0 if grid_perc_h20 == .
replace mt_binary_mean = 0 if mt_binary_mean == .

*After calculating the weighted average for an area, divide values by 100,000 to obtain the Terrain Ruggedness Index in hundreds of metres. Average slope is in thousandths of a percentage point in the 30 arc-seconds grid as opposed to percentage points in the country-level data. 
*After calculating the weighted average for an area, divide values by 1,000 to obtain average slope in percent.
gen Rugged_SlopeFinal =(rugged_slopettl/rugged_cellareattl)/1000
gen Rugged_RuggednessFinal =(rugged_trittl/rugged_cellareattl)/100000

bysort grid_id: gen N = _n
bysort grid_id: egen maxN = max(N)
gen keep1 = 1 if maxN == N
bysort grid_id: gen Mark = 1 if country == "South Sudan"
bysort grid_id: egen maxMark = max(Mark)
gen Drop1 = 1 if  maxN == 2 & country == "Sudan" & maxMark == 1
drop if Drop1 ==1 
drop if keep1 == .

replace d_coast_m = subinstr(d_coast_m, " [m]", "",.) 
gen d_coast = real(d_coast_m)

gen D_Coast_km = d_coast/1000

rename img_prob_p EWI_p_poor
rename img_prob_l EWI_p_lowincome
rename img_prob_u EWI_p_upperincome
rename img_prob_r EWI_p_rich
rename estimated_ EWI_Index

gen Min_State_Dist1966 = min(natcap1966, regcap1966)
gen Min_State_Dist1968 = min(natcap1968, regcap1968)
gen Min_State_Dist1975 = min(natcap1975, regcap1975)
gen Min_State_Dist1977 = min(natcap1977, regcap1977)
gen Min_State_Dist1980 = min(natcap1980, regcap1980)

gen Min_D_State_Indep = Min_State_Dist1966
replace Min_D_State_Indep = Min_State_Dist1968 if country == "Equatorial Guinea" | country == "Swaziland"
replace Min_D_State_Indep = Min_State_Dist1975 if country == "Angola" | country == "Mozambique"
replace Min_D_State_Indep = Min_State_Dist1977 if country == "Djibouti"
replace Min_D_State_Indep = Min_State_Dist1980 if country == "Zimbabwe"

collapse (first) Min_D_State_Indep  EWI_p_poor EWI_p_lowincome EWI_p_upperincome EWI_p_rich  EWI_Index popdensity_0ad gems  silver iron copper tin gold country country_area_km D_Coast_km Rugged_SlopeFinal Rugged_RuggednessFinal grid_longitude grid_latitude grid_area_km water_area_km2 grid_perc_h20  nightlight_20132_mean diamonds mt_binary_mean grid_mttype malaria_2000  elevation landsuitability  precipitation rugged_cellareattl rugged_slopettl rugged_trittl cellarea, by(grid_id)

sort grid_id
merge grid_id using "Appendix/Larger Grids/White_Eco_25.dta"

rename gridcode Whites_id

hhi white_ecoarea, by(grid_id)
rename hhi_white_ecoarea Eco_Diversity

replace Eco_Diversity = . if white_ecoarea == .

collapse (first) Min_D_State_Indep Eco_Diversity EWI_p_poor EWI_p_lowincome EWI_p_upperincome EWI_p_rich  EWI_Index popdensity_0ad gems  silver iron copper tin gold country country_area_km D_Coast_km Rugged_SlopeFinal Rugged_RuggednessFinal grid_longitude grid_latitude grid_area_km water_area_km2 grid_perc_h20  nightlight_20132_mean diamonds mt_binary_mean grid_mttype malaria_2000  elevation landsuitability  precipitation rugged_cellareattl rugged_slopettl rugged_trittl cellarea, by(grid_id)

sort grid_id
save "Appendix/Larger Grids/Controls_GRID_25degrees.dta", replace


import delimited "Appendix/Larger Grids/Dist_Capitals_25degree.csv", varnames(1) clear
rename inputid grid_id
rename targetid country_capital
gen dist_capital_km = distance/1000

replace country_capital = "Swaziland" if country_capital == "eSwatini"
replace country_capital = "Gambia" if country_capital == "The Gambia"
replace country_capital = "Congo-Brazzaville" if country_capital == "Congo (Brazzaville)"
replace country_capital = "Democratic Republic of Congo" if country_capital == "Congo (Kinshasa)"
replace country_capital = "Cote d`Ivoire" if country_capital == "Ivory Coast"
replace country_capital = "Somalia" if country_capital == "Somaliland"
replace country_capital = "Guinea-Bissau" if country_capital == "Guinea Bissau"

keep grid_id country_capital dist_capital_km 
sort grid_id
merge grid_id using "Appendix/Larger Grids/Controls_GRID_25degrees.dta"
drop _merge

bysort grid_id: gen Match = 0
bysort grid_id: replace Match = 1 if country == country_capital
bysort grid_id: replace Match = 1 if country == "South Sudan" & country_capital == "Sudan"

bysort grid_id: gen Match2 = 1 if country == "South Sudan" & country_capital == "Sudan"

drop if Match == 0

gen dist_capital_km_2 = .
replace dist_capital_km_2 = dist_capital_km if Match2 == 1
bysort grid_id: egen dist_capital_km_pre2011 = max(dist_capital_km_2) 
drop if Match2 == 1

bysort grid_id: egen min_dist = min(dist_capital_km)
keep if min_dist == dist_capital_km

replace dist_capital_km_pre2011 = dist_capital_km if dist_capital_km_pre2011 == .

drop dist_capital_km_2

sort grid_id
save "Appendix/Larger Grids/Controls_GRID_Final_25degrees.dta", replace
}

*IMPORT POLITIES
{
import delimited "Appendix/Larger Grids/Atlas_25degrees.csv", varnames(1) clear 
rename grid_area_kmxx grid_area_km
rename water_area_km2x water_area_km2
rename grid_idx grid_id
keep  atlas_8hr_area_km_intersect grid_area_km  water_area_km2 polity_8hr_clip   grid_id

*8hr
gen Ndebele = 1 if polity_8hr == "Ndebele"
bysort grid_id: egen Ndebele_Any = max(Ndebele)
gen Macina = 1 if polity_8hr == "Macina"
bysort grid_id: egen Macina_Any = max(Macina)
gen Segou = 1 if polity_8hr == "Segou"
bysort grid_id: egen Segou_Any = max(Segou)
gen Kaarta = 1 if polity_8hr == "Kaarta"
bysort grid_id: egen Kaarta_Any = max(Kaarta)
gen Harar = 1 if polity_8hr == "Harar"
bysort grid_id: egen Harar_Any = max(Harar)
gen Dawro = 1 if polity_8hr == "Dawro"
bysort grid_id: egen Dawro_Any = max(Dawro)
gen Gera = 1 if polity_8hr == "Gera"
bysort grid_id: egen Gera_Any = max(Gera)
gen Gomma = 1 if polity_8hr == "Gomma"
bysort grid_id: egen Gomma_Any = max(Gomma)
gen Gumma = 1 if polity_8hr == "Gumma"
bysort grid_id: egen Gumma_Any = max(Gumma)
gen Janjera = 1 if polity_8hr == "Janjera"
bysort grid_id: egen Janjera_Any = max(Janjera)
gen Jimma = 1 if polity_8hr == "Jimma"
bysort grid_id: egen Jimma_Any = max(Jimma)
gen Kaffa = 1 if polity_8hr == "Kaffa"
bysort grid_id: egen Kaffa_Any = max(Kaffa)
gen Konta = 1 if polity_8hr == "Konta"
bysort grid_id: egen Konta_Any = max(Konta)
gen LeqaNaqamt = 1 if polity_8hr == "LeqaNaqamt"
bysort grid_id: egen LeqaNaqamt_Any = max(LeqaNaqamt)
gen LeqaQellem = 1 if polity_8hr == "LeqaQellem"
bysort grid_id: egen LeqaQellem_Any = max(LeqaQellem)
gen LimmuEnnar = 1 if polity_8hr == "LimmuEnnar"
bysort grid_id: egen LimmuEnnar_Any = max(LimmuEnnar)
gen Shewa = 1 if polity_8hr == "Shewa"
bysort grid_id: egen Shewa_Any = max(Shewa)
gen Wolayta = 1 if polity_8hr == "Wolayta"
bysort grid_id: egen Wolayta_Any = max(Wolayta)
gen Anfillo = 1 if polity_8hr == "Anfillo"
bysort grid_id: egen Anfillo_Any = max(Anfillo)

gen Double8 = 1 if polity_8hr == "Rozvi" & Ndebele_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Macina_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Segou_Any == 1
replace Double8 = 1 if polity_8hr == "Samory" & Segou_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Kaarta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Harar_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Dawro_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gera_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gomma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gumma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Janjera_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Jimma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Konta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & LeqaNaqamt_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & LeqaQellem_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Shewa_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Wolayta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Kaffa_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Anfillo_Any == 1

replace atlas_8hr_area_km_intersect = 0 if Double8 == 1
bysort grid_id: egen max_Polity = max(atlas_8hr_area_km_intersect) if Double8 != 1

gen Polity_Clip_8hr = ""
gen Polity_Clip_8hr_area = .
replace Polity_Clip_8hr = polity_8hr_clip if max_Polity == atlas_8hr_area_km_intersect
replace Polity_Clip_8hr_area = atlas_8hr_area_km_intersect if max_Polity == atlas_8hr_area_km_intersect 

bysort grid_id (Polity_Clip_8hr): replace Polity_Clip_8hr = Polity_Clip_8hr[_N] if missing(Polity_Clip_8hr) 
bysort grid_id (Polity_Clip_8hr_area): replace Polity_Clip_8hr_area = Polity_Clip_8hr_area[1] if missing(Polity_Clip_8hr_area) 

drop max_Polity Ndebele Ndebele_Any Double8 Macina Macina_Any Segou Segou_Any Kaarta Kaarta_Any Harar Harar_Any Dawro Dawro_Any Gera Gera_Any Gomma Gomma_Any Gumma Gumma_Any Janjera Janjera_Any Jimma Jimma_Any Kaffa Kaffa_Any Konta Konta_Any LeqaNaqamt LeqaNaqamt_Any LeqaQellem LeqaQellem_Any LimmuEnnar LimmuEnnar_Any Shewa Shewa_Any Wolayta Wolayta_Any Anfillo Anfillo_Any

collapse (first) grid_area_km  Polity_Clip_8hr Polity_Clip_8hr_area , by(grid_id)

gen Overlap8hr = Polity_Clip_8hr_area/grid_area_km
replace Polity_Clip_8hr = "" if Overlap8hr < 0.25

drop  Overlap8hr 

sort grid_id
save "Appendix/Larger Grids/PolitiesClip_GRID_Final_25degrees.dta", replace
}

**** MERGE ALL TOGETHER
{
use "Appendix/Larger Grids/Controls_GRID_Final_25degrees.dta", clear
sort grid_id
merge grid_id using "Appendix/Larger Grids/PolitiesClip_GRID_Final_25degrees.dta"
drop _merge
save "Appendix/Larger Grids/Polities_GRID_25_Final.dta", replace


** MERGE IN POLITY DATA
import delimited "Appendix/Larger Grids/Grid_0_5_degree_MERGEE_25km.csv", clear 
bysort grid_id: egen max_grid = max(grid_5deg_area_km)
keep if grid_5deg_area_km == max_grid
sort grid_id
save "Appendix/Larger Grids/Grid_25deg_Grid_05_grid.dta", replace

import delimited "Appendix/Larger Grids/Grid_0_5_degree_MERGEE_55km.csv", clear 
rename grid0_5_id grid_id
bysort grid_id: egen max_grid = max(grid_5deg_area_km)
keep if grid_5deg_area_km == max_grid
sort grid_id
save "Appendix/Larger Grids/Grid_55deg_Grid_05_grid.dta", replace

import delimited "Appendix/Larger Grids/Grid_1_degree_MERGE_25km.csv", clear 
bysort grid_id: egen max_grid = max(grid_1deg_area_km)
keep if grid_1deg_area_km == max_grid
sort grid_id
save "Appendix/Larger Grids/Grid_25deg_Grid_1_grid.dta", replace

import delimited "Appendix/Larger Grids/Grid_1_degree_MERGE_55km.csv", clear 
rename grid0_5_id grid_id
bysort grid_id: egen max_grid = max(grid_1deg_area_km)
keep if grid_1deg_area_km == max_grid
sort grid_id
save "Appendix/Larger Grids/Grid_55deg_Grid_1_grid.dta", replace

import delimited "Appendix/Larger Grids/Grid_2_degree_MERGEE_25km.csv", clear 
bysort grid_id: egen max_grid = max(grid_2deg_area_km)
keep if grid_2deg_area_km == max_grid
sort grid_id
save "Appendix/Larger Grids/Grid_25deg_Grid_2_grid.dta", replace

import delimited "Appendix/Larger Grids/Grid_2_degree_MERGEE_55km.csv", clear 
rename grid0_5_id grid_id
bysort grid_id: egen max_grid = max(grid_2deg_area_km)
keep if grid_2deg_area_km == max_grid
sort grid_id
save "Appendix/Larger Grids/Grid_55deg_Grid_2_grid.dta", replace


use "Appendix/Larger Grids/Polities_GRID_25_Final.dta", clear
rename Polity_Clip_8hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_8hr_Clip
rename Elite_Structure LineageStruc_8hr_Clip
rename N_Sources N_Sources_8hr_Clip
rename Polity Polity_Clip_8hr

sort grid_id
merge grid_id using "Appendix/Larger Grids/GED_GRID_Final_25degrees.dta"
drop _merge
sort grid_id
merge grid_id using "Appendix/Larger Grids/Acled_GRID_Final_25degrees.dta"
drop _merge
sort grid_id
merge grid_id using "Appendix/Larger Grids/MetaWealth_25km_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Appendix/Larger Grids/Controls_GRID_Final_25degrees.dta"
drop _merge

gen ln_d_coast_km = log(D_Coast_km) 

gen ln__mean2013 = ln(nightlight_20132_mean+0.01)

gen Centralized_8hr_Clip = 0
replace Centralized_8hr_Clip = 1 if Polity_Clip_8hr != ""

gen Polity_Type_8hr_Clip = 0
replace Polity_Type_8hr_Clip = 1 if LineageStruc_8hr_Clip == "Federation"
replace Polity_Type_8hr_Clip = 2 if LineageStruc_8hr_Clip == "Gatekeeper"
replace Polity_Type_8hr_Clip = 2 if LineageStruc_8hr_Clip == "gatekeeper"
replace Polity_Type_8hr_Clip = 3 if LineageStruc_8hr_Clip == "Regal"
replace Polity_Type_8hr_Clip = 4 if LineageStruc_8hr_Clip == "Despotic"

gen Polity_Type_Fed = 0
replace Polity_Type_Fed = 1 if Polity_Type_8hr_Clip == 1
gen Polity_Type_Gate = 0
replace Polity_Type_Gate = 1 if Polity_Type_8hr_Clip == 2
gen Polity_Type_Regal = 0
replace Polity_Type_Regal = 1 if Polity_Type_8hr_Clip == 3
gen Polity_Type_Desp = 0
replace Polity_Type_Desp = 1 if Polity_Type_8hr_Clip == 4

sort grid_id
merge grid_id using "Appendix/Larger Grids/Grid_25deg_Grid_2_grid.dta"
drop _merge
sort grid_id 
merge grid_id using "Appendix/Larger Grids/Grid_25deg_Grid_1_grid.dta"
drop _merge
sort grid_id 
merge grid_id using "Appendix/Larger Grids/Grid_25deg_Grid_05_grid.dta"
drop _merge

rename precipitation CHIRPS_Prec_mm

gen Diamonds = 0
replace Diamonds = 1 if diamonds != .
gen Gems = 0
replace Gems = 1 if gems != .
gen Silver = 0
replace Silver = 1 if silver != .
gen Iron = 0
replace Iron = 1 if iron != .
gen Copper = 0
replace Copper = 1 if copper != .
gen Tin = 0
replace Tin = 1 if tin != .
gen Gold = 0
replace Gold = 1 if gold != .

drop gold tin copper iron silver gems diamonds

gen NaturalResources = 0
replace NaturalResource = (Diamonds+Gems+Silver+Iron+Copper+Tin+Gold)

gen NaturalResources_Dummy = 0
replace NaturalResources_Dummy =1 if NaturalResources > 0

gen ln_popdensity_0ad = log(0.001+popdensity_0ad)
gen ln_dist_capital_km = log(0.001+dist_capital_km)
gen ln_dist_capital_km_pre2011 = log(0.001+dist_capital_km_pre2011)

replace EWI_Index = EWI_Index/100

gen grid_longitude2 = grid_longitude*grid_longitude
gen grid_latitude2 = grid_latitude*grid_latitude

gen ln_CHIRPS_Prec_mm = log(0.001+CHIRPS_Prec_mm)
label var ln_CHIRPS_Prec_mm "ln Prec (mm)"

sum CHIRPS_Prec_mm  mt_binary_mean grid_perc_h20 elevation Rugged_RuggednessFinal grid_longitude grid_latitude D_Coast_km landsuitability  
sum CHIRPS_Prec_mm  mt_binary_mean grid_perc_h20 elevation Rugged_RuggednessFinal grid_longitude grid_latitude D_Coast_km landsuitability  if Centralized_8hr == 1

gen Counterfactual_25 = 1 
replace Counterfactual_25 = 0 if elevation <  1.595828
replace Counterfactual_25 = 0 if Rugged_RuggednessFinal > 12.34718
replace Counterfactual_25 = 0 if grid_latitude <  -30.57956
replace Counterfactual_25 = 0 if grid_latitude > 19.67044
replace Counterfactual_25 = 0 if CHIRPS_Prec_mm >  3472.797
replace Counterfactual_25 = 0 if CHIRPS_Prec_mm <  6.347549
replace Counterfactual_25 = 0 if D_Coast_km <  .0791719

save "Appendix/Larger Grids/FINAL_CLIPPED_25degrees.dta", replace
}

*******************************************************************
**************** 55 DEGREE GRIDS *********************************
*******************************************************************

********** 55km
{
import delimited "Appendix/Larger Grids/AcledID_55degrees.csv", varnames(1) clear 
keep acled_id grid_id
save "Appendix/Larger Grids/Base_DVs1_55degrees.dta", replace


use "Appendix/Larger Grids/Base_DVs1_55degrees.dta", clear
sort acled_id
merge acled_id using "Data/Calculated Output/Acled_Merge.dta"

gen Battles = 0
replace Battles = 1 if event_type == "Battles"
gen Riots = 0
replace Riots = 1 if event_type == "Riots"
gen Civ_Violence = 0
replace Civ_Violence = 1 if event_type == "Violence against civilians"

gen Battles_pre2013 = 0
replace Battles_pre2013 = 1 if event_type == "Battles" & year < 2014
gen Remote_pre2013 = 0
replace Remote_pre2013 = 1 if event_type == "Explosions/Remote violence" & year < 2014

gen State_Actor = 0
replace State_Actor = 1 if interaction == 12 | interaction == 13 | interaction == 14 | interaction == 23 | interaction == 33 | interaction == 34 

replace Battles_pre2013 = 0 if State_Actor == 0  & year < 2014
replace Remote_pre2013 = 0 if State_Actor == 0  & year < 2014

gen ViolenceSum = Battles_pre2013+Remote_pre2013

bysort grid_id year: gen N_yrs_conflict_Acled_pre2013 = 1 if ViolenceSum > 0 
bysort grid_id year N_yrs_conflict_Acled_pre2013: gen N2 = _n 
bysort grid_id year N_yrs_conflict_Acled_pre2013: egen maxN2 = max(N2)
replace N_yrs_conflict_Acled_pre2013 = 0 if N2 > 1

collapse (rawsum) N_yrs_conflict_Acled_pre2013 Battles_pre2013 Remote_pre2013 Civ_Violence Battles Riots, by(grid_id)

gen Agg_Acled = Riots+Battles+Civ_Violence
gen ln_Agg_Acled = log(1+Agg_Acled)

gen Agg_Acled_Violence = Battles+Civ_Violence
gen ln_Agg_Acled_Violence = log(1+Agg_Acled_Violence)

replace Battles_pre2013 = 0 if Battles_pre2013 == .
replace Remote_pre2013 = 0 if Remote_pre2013 == .
replace N_yrs_conflict_Acled_pre2013 = 0 if N_yrs_conflict_Acled_pre2013 == .

gen Agg_Acled_pre2013 = Battles_pre2013+Remote_pre2013
gen ln_Agg_Acled_pre2013 = log(1+Agg_Acled_pre2013)
gen ln_N_yrs_conflict_Acled_pre2013 = log(1+N_yrs_conflict_Acled_pre2013)

save "Appendix/Larger Grids/Acled_GRID_Final_55degrees.dta", replace
}

* Import, clean, match GED data
{
import delimited "Appendix/Larger Grids/GEDID_55degrees.csv", varnames(1) clear 
rename grid0_5_idx grid_id
keep ged_id grid_id
save "Appendix/Larger Grids/Base_DVs2_55degrees.dta", replace

use "Appendix/Larger Grids/Base_DVs2_55degrees.dta", clear
sort ged_id
merge ged_id using "Data/Calculated Output/GED_Merge.dta"
drop if _merge == 2

collapse (first) year active_year type_of_violence conflict_new_id where_prec N_Battles_State N_Battles_NonState N_Battles_Total N_Battles_State_pre2013 N_Battles_NonState_pre2013 N_Battles_Total_pre2013, by (ged_id grid_id)

bysort grid_id conflict_new_id: gen N_Conflicts = 1
bysort grid_id conflict_new_id: gen N = _n
replace N_Conflicts = 0 if N > 1

bysort grid_id conflict_new_id: gen N_Conflicts_pre2013 = 1 if year < 2014
bysort grid_id conflict_new_id: gen N2 = _n if year < 2014
replace N_Conflicts_pre2013 = 0 if N2 > 1 & year < 2014

bysort grid_id year: gen N_yrs_conflict = 1 if active_year == 1
bysort grid_id year: gen N3= _n
bysort grid_id year: replace N_yrs_conflict = 0 if N3 > 1

bysort grid_id year: gen N_yrs_conflict_pre2013 = 1 if active_year == 1 
bysort grid_id year: gen N4 = _n 
replace N_yrs_conflict_pre2013 = 0 if N4 > 1 
replace N_yrs_conflict_pre2013 = . if  year > 2013

bysort grid_id conflict_new_id: gen N_StateConflicts = 1 if active_year == 1 & type_of_violence == 1
bysort grid_id conflict_new_id: gen N5 = _n
replace N_StateConflicts = 0 if N5 > 1

bysort grid_id conflict_new_id: gen N_StateConflicts_pre2013 = 1 if active_year == 1 & type_of_violence == 1 
bysort grid_id conflict_new_id: gen N6 = _n 
replace N_StateConflicts_pre2013 = 0 if N6 > 1 

bysort grid_id year: gen N_yrs_Stateconflict = 1 if active_year == 1 & type_of_violence == 1
bysort grid_id year: gen N7 = _n
replace N_yrs_Stateconflict = 0 if N7 > 1

bysort grid_id year: gen N_yrs_Stateconflict_pre2013  = 1 if active_year == 1 & type_of_violence == 1 
bysort grid_id year: gen N8 = _n 
replace N_yrs_Stateconflict_pre2013  = 0 if N8 > 1 
replace N_yrs_Stateconflict_pre2013 = . if  year > 2013

collapse (rawsum) N_yrs_Stateconflict_pre2013 N_Conflicts_pre2013 N_StateConflicts_pre2013 N_yrs_conflict_pre2013 N_Battles_Total_pre2013 N_Battles_State_pre2013 N_Battles_NonState_pre2013  N_Conflicts N_yrs_conflict N_Battles_Total N_Battles_State N_Battles_NonState, by(grid_id)

gen ln_N_yrs_conflict_pre2013 = log(1+N_yrs_conflict_pre2013)
gen ln_N_Battles_Total_pre2013 = log(1+N_Battles_Total_pre2013)
gen ln_N_Conflicts_pre2013 = log(1+N_Conflicts_pre2013)
gen ln_N_Battles_State_pre2013 = log(1+N_Battles_State_pre2013)
gen ln_N_StateConflicts_pre2013 = log(1+N_StateConflicts_pre2013)
gen ln_N_yrs_Stateconflict_pre2013 = log(1+N_yrs_Stateconflict_pre2013)

save "Appendix/Larger Grids/GED_GRID_Final_55degrees.dta", replace
}

* Import, clean, match Meta Wealth Data
{
import delimited "Appendix/Larger Grids/Meta_Wealth_55degrees.csv", varnames(1) clear 

collapse (mean) rwi, by(grid_id)

sort grid_id
save "Appendix/Larger Grids/MetaWealth_55km_Final.dta", replace
}

* Controls
{
import delimited "Appendix/Larger Grids/White_Eco_55.csv", varnames(1) clear 
rename grid0_5_id grid_id
sort grid_id
save "Appendix/Larger Grids/White_Eco_55.dta", replace

import delimited "Appendix/Larger Grids/GEDID_55degrees.csv", varnames(1) clear 

rename centroids1 grid_longitude
rename centroids2 grid_latitude
rename grid_perch20x grid_perc_h20
rename grid_area_kmxx grid_area_km
rename water_area_km2x water_area_km2
rename countryx country
rename country_area_kmx country_area_km
rename nightlight_20132_meany nightlight_20132_mean
rename grid0_5_idx grid_id

replace water_area_km2 = 0 if water_area_km2 == .
replace grid_perc_h20 = 0 if grid_perc_h20 == .
replace mt_binary_mean = 0 if mt_binary_mean == .

gen Rugged_SlopeFinal =(rugged_slopettl/rugged_cellareattl)/1000
gen Rugged_RuggednessFinal =(rugged_trittl/rugged_cellareattl)/100000

bysort grid_id: gen N = _n
bysort grid_id: egen maxN = max(N)
gen keep1 = 1 if maxN == N
bysort grid_id: gen Mark = 1 if country == "South Sudan"
bysort grid_id: egen maxMark = max(Mark)
gen Drop1 = 1 if  maxN == 2 & country == "Sudan" & maxMark == 1
drop if Drop1 ==1 
drop if keep1 == .

replace d_coast_m = subinstr(d_coast_m, " [m]", "",.) 
gen d_coast = real(d_coast_m)

gen D_Coast_km = d_coast/1000

rename img_prob_p EWI_p_poor
rename img_prob_l EWI_p_lowincome
rename img_prob_u EWI_p_upperincome
rename img_prob_r EWI_p_rich
rename estimated_ EWI_Index

gen Min_State_Dist1966 = min(natcap1966, regcap1966)
gen Min_State_Dist1968 = min(natcap1968, regcap1968)
gen Min_State_Dist1975 = min(natcap1975, regcap1975)
gen Min_State_Dist1977 = min(natcap1977, regcap1977)
gen Min_State_Dist1980 = min(natcap1980, regcap1980)

gen Min_D_State_Indep = Min_State_Dist1966
replace Min_D_State_Indep = Min_State_Dist1968 if country == "Equatorial Guinea" | country == "Swaziland"
replace Min_D_State_Indep = Min_State_Dist1975 if country == "Angola" | country == "Mozambique"
replace Min_D_State_Indep = Min_State_Dist1977 if country == "Djibouti"
replace Min_D_State_Indep = Min_State_Dist1980 if country == "Zimbabwe"

collapse (first) Min_D_State_Indep EWI_p_poor EWI_p_lowincome EWI_p_upperincome EWI_p_rich  EWI_Index popdensity_0ad country country_area_km D_Coast_km Rugged_SlopeFinal Rugged_RuggednessFinal grid_longitude grid_latitude grid_area_km water_area_km2 grid_perc_h20    nightlight_20132_mean diamonds gems silver copper tin iron gold mt_binary_mean grid_mttype malaria_2000  elevation landsuitability precipitation rugged_cellareattl rugged_slopettl rugged_trittl cellarea, by(grid_id)

sort grid_id
merge grid_id using "Appendix/Larger Grids/White_Eco_25.dta"

rename gridcode Whites_id

hhi white_ecoarea, by(grid_id)
rename hhi_white_ecoarea Eco_Diversity

replace Eco_Diversity = . if white_ecoarea == .

collapse (first) Min_D_State_Indep Eco_Diversity EWI_p_poor EWI_p_lowincome EWI_p_upperincome EWI_p_rich  EWI_Index popdensity_0ad country country_area_km D_Coast_km Rugged_SlopeFinal Rugged_RuggednessFinal grid_longitude grid_latitude grid_area_km water_area_km2 grid_perc_h20    nightlight_20132_mean diamonds gems silver copper tin iron gold mt_binary_mean grid_mttype malaria_2000  elevation landsuitability precipitation rugged_cellareattl rugged_slopettl rugged_trittl cellarea, by(grid_id)

sort grid_id
save "Appendix/Larger Grids/Controls_GRID_55degrees.dta", replace


import delimited "Appendix/Larger Grids/Dist_Capitals_55degree.csv", varnames(1) clear
rename inputid grid_id
rename targetid country_capital
gen dist_capital_km = distance/1000

replace country_capital = "Swaziland" if country_capital == "eSwatini"
replace country_capital = "Gambia" if country_capital == "The Gambia"
replace country_capital = "Congo-Brazzaville" if country_capital == "Congo (Brazzaville)"
replace country_capital = "Democratic Republic of Congo" if country_capital == "Congo (Kinshasa)"
replace country_capital = "Cote d`Ivoire" if country_capital == "Ivory Coast"
replace country_capital = "Somalia" if country_capital == "Somaliland"
replace country_capital = "Guinea-Bissau" if country_capital == "Guinea Bissau"

keep grid_id country_capital dist_capital_km 
sort grid_id
merge grid_id using "Appendix/Larger Grids/Controls_GRID_55degrees.dta"
drop _merge

bysort grid_id: gen Match = 0
bysort grid_id: replace Match = 1 if country == country_capital
bysort grid_id: replace Match = 1 if country == "South Sudan" & country_capital == "Sudan"

bysort grid_id: gen Match2 = 1 if country == "South Sudan" & country_capital == "Sudan"

drop if Match == 0

gen dist_capital_km_2 = .
replace dist_capital_km_2 = dist_capital_km if Match2 == 1
bysort grid_id: egen dist_capital_km_pre2011 = max(dist_capital_km_2) 
drop if Match2 == 1

bysort grid_id: egen min_dist = min(dist_capital_km)
keep if min_dist == dist_capital_km

replace dist_capital_km_pre2011 = dist_capital_km if dist_capital_km_pre2011 == .

drop dist_capital_km_2 min_dist Match

sort grid_id
save "Appendix/Larger Grids/Controls_GRID_Final_55degrees.dta", replace

}

*IMPORT POLITIES
{
import delimited "Appendix/Larger Grids/Atlas_55degree.csv", varnames(1) clear 
rename grid_area_kmxx grid_area_km
rename grid0_5_idx grid_id
keep grid_area_km  polity_8hr_clip atlas_8hr_area_km_intersect grid_id

*8hr
gen Ndebele = 1 if polity_8hr == "Ndebele"
bysort grid_id: egen Ndebele_Any = max(Ndebele)
gen Macina = 1 if polity_8hr == "Macina"
bysort grid_id: egen Macina_Any = max(Macina)
gen Segou = 1 if polity_8hr == "Segou"
bysort grid_id: egen Segou_Any = max(Segou)
gen Kaarta = 1 if polity_8hr == "Kaarta"
bysort grid_id: egen Kaarta_Any = max(Kaarta)
gen Harar = 1 if polity_8hr == "Harar"
bysort grid_id: egen Harar_Any = max(Harar)
gen Dawro = 1 if polity_8hr == "Dawro"
bysort grid_id: egen Dawro_Any = max(Dawro)
gen Gera = 1 if polity_8hr == "Gera"
bysort grid_id: egen Gera_Any = max(Gera)
gen Gomma = 1 if polity_8hr == "Gomma"
bysort grid_id: egen Gomma_Any = max(Gomma)
gen Gumma = 1 if polity_8hr == "Gumma"
bysort grid_id: egen Gumma_Any = max(Gumma)
gen Janjera = 1 if polity_8hr == "Janjera"
bysort grid_id: egen Janjera_Any = max(Janjera)
gen Jimma = 1 if polity_8hr == "Jimma"
bysort grid_id: egen Jimma_Any = max(Jimma)
gen Kaffa = 1 if polity_8hr == "Kaffa"
bysort grid_id: egen Kaffa_Any = max(Kaffa)
gen Konta = 1 if polity_8hr == "Konta"
bysort grid_id: egen Konta_Any = max(Konta)
gen LeqaNaqamt = 1 if polity_8hr == "LeqaNaqamt"
bysort grid_id: egen LeqaNaqamt_Any = max(LeqaNaqamt)
gen LeqaQellem = 1 if polity_8hr == "LeqaQellem"
bysort grid_id: egen LeqaQellem_Any = max(LeqaQellem)
gen LimmuEnnar = 1 if polity_8hr == "LimmuEnnar"
bysort grid_id: egen LimmuEnnar_Any = max(LimmuEnnar)
gen Shewa = 1 if polity_8hr == "Shewa"
bysort grid_id: egen Shewa_Any = max(Shewa)
gen Wolayta = 1 if polity_8hr == "Wolayta"
bysort grid_id: egen Wolayta_Any = max(Wolayta)
gen Anfillo = 1 if polity_8hr == "Anfillo"
bysort grid_id: egen Anfillo_Any = max(Anfillo)

gen Double8 = 1 if polity_8hr == "Rozvi" & Ndebele_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Macina_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Segou_Any == 1
replace Double8 = 1 if polity_8hr == "Samory" & Segou_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Kaarta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Harar_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Dawro_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gera_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gomma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gumma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Janjera_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Jimma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Konta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & LeqaNaqamt_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & LeqaQellem_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Shewa_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Wolayta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Kaffa_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Anfillo_Any == 1

replace atlas_8hr_area_km_intersect = 0 if Double8 == 1
bysort grid_id: egen max_Polity = max(atlas_8hr_area_km_intersect) if Double8 != 1

gen Polity_Clip_8hr = ""
gen Polity_Clip_8hr_area = .
replace Polity_Clip_8hr = polity_8hr_clip if max_Polity == atlas_8hr_area_km_intersect
replace Polity_Clip_8hr_area = atlas_8hr_area_km_intersect if max_Polity == atlas_8hr_area_km_intersect 

bysort grid_id (Polity_Clip_8hr): replace Polity_Clip_8hr = Polity_Clip_8hr[_N] if missing(Polity_Clip_8hr) 
bysort grid_id (Polity_Clip_8hr_area): replace Polity_Clip_8hr_area = Polity_Clip_8hr_area[1] if missing(Polity_Clip_8hr_area) 

drop max_Polity Ndebele Ndebele_Any Double8 Macina Macina_Any Segou Segou_Any Kaarta Kaarta_Any Harar Harar_Any Dawro Dawro_Any Gera Gera_Any Gomma Gomma_Any Gumma Gumma_Any Janjera Janjera_Any Jimma Jimma_Any Kaffa Kaffa_Any Konta Konta_Any LeqaNaqamt LeqaNaqamt_Any LeqaQellem LeqaQellem_Any LimmuEnnar LimmuEnnar_Any Shewa Shewa_Any Wolayta Wolayta_Any Anfillo Anfillo_Any

collapse (first) grid_area_km   Polity_Clip_8hr Polity_Clip_8hr_area , by(grid_id)

gen Overlap8hr = Polity_Clip_8hr_area/grid_area_km
replace Polity_Clip_8hr = "" if Overlap8hr < 0.25

drop  Overlap8hr 

sort grid_id
save "Appendix/Larger Grids/PolitiesClip_GRID_Final_55degrees.dta", replace
}

**** MERGE ALL TOGETHER
{
use "Appendix/Larger Grids/Controls_GRID_Final_55degrees.dta", clear
sort grid_id
merge grid_id using "Appendix/Larger Grids/PolitiesClip_GRID_Final_55degrees.dta"
drop _merge
save "Appendix/Larger Grids/Polities_GRID_55_Final.dta", replace


** MERGE IN POLITY DATA
use "Appendix/Larger Grids/Polities_GRID_55_Final.dta", clear

rename Polity_Clip_8hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_8hr_Clip
rename Elite_Structure LineageStruc_8hr_Clip
rename N_Sources N_Sources_8hr_Clip
rename Polity Polity_Clip_8hr

sort grid_id
merge grid_id using "Appendix/Larger Grids/GED_GRID_Final_55degrees.dta"
drop _merge
sort grid_id
merge grid_id using "Appendix/Larger Grids/Acled_GRID_Final_55degrees.dta"
drop _merge
sort grid_id
merge grid_id using "Appendix/Larger Grids/MetaWealth_55km_Final.dta"
drop _merge

gen ln_d_coast_km = log(D_Coast_km) 
gen ln_mean2013 = ln(nightlight_20132_mean+0.01)
gen ln_dist_capital_km = ln(dist_capital_km+0.01)
gen ln_dist_capital_km_pre2011 = ln(dist_capital_km_pre2011+0.01)
gen ln_popdensity_0ad = ln(popdensity_0ad+0.01)

gen Centralized_8hr_Clip = 0
replace Centralized_8hr_Clip = 1 if Polity_Clip_8hr != ""

gen Polity_Type_8hr_Clip = 0
replace Polity_Type_8hr_Clip = 1 if LineageStruc_8hr_Clip == "Federation"
replace Polity_Type_8hr_Clip = 2 if LineageStruc_8hr_Clip == "Gatekeeper"
replace Polity_Type_8hr_Clip = 2 if LineageStruc_8hr_Clip == "gatekeeper"
replace Polity_Type_8hr_Clip = 3 if LineageStruc_8hr_Clip == "Regal"
replace Polity_Type_8hr_Clip = 4 if LineageStruc_8hr_Clip == "Despotic"

gen Polity_Type_Fed = 0
replace Polity_Type_Fed = 1 if Polity_Type_8hr_Clip == 1
gen Polity_Type_Gate = 0
replace Polity_Type_Gate = 1 if Polity_Type_8hr_Clip == 2
gen Polity_Type_Regal = 0
replace Polity_Type_Regal = 1 if Polity_Type_8hr_Clip == 3
gen Polity_Type_Desp = 0
replace Polity_Type_Desp = 1 if Polity_Type_8hr_Clip == 4

sort grid_id
merge grid_id using "Appendix/Larger Grids/Grid_55deg_Grid_2_grid.dta"
drop _merge
sort grid_id 
merge grid_id using "Appendix/Larger Grids/Grid_55deg_Grid_1_grid.dta"
drop _merge
sort grid_id 
merge grid_id using "Appendix/Larger Grids/Grid_55deg_Grid_05_grid.dta"
drop _merge

rename precipitation CHIRPS_Prec_mm

gen Diamonds = 0
replace Diamonds = 1 if diamonds != .
gen Gems = 0
replace Gems = 1 if gems != .
gen Silver = 0
replace Silver = 1 if silver != .
gen Iron = 0
replace Iron = 1 if iron != .
gen Copper = 0
replace Copper = 1 if copper != .
gen Tin = 0
replace Tin = 1 if tin != .
gen Gold = 0
replace Gold = 1 if gold != .

drop gold tin copper iron silver gems diamonds

gen NaturalResources = 0
replace NaturalResource = (Diamonds+Gems+Silver+Iron+Copper+Tin+Gold)

gen NaturalResources_Dummy = 0
replace NaturalResources_Dummy =1 if NaturalResources > 0

replace EWI_Index = EWI_Index/100

gen grid_longitude2 = grid_longitude*grid_longitude
gen grid_latitude2 = grid_latitude*grid_latitude

sum CHIRPS_Prec_mm  mt_binary_mean grid_perc_h20 elevation Rugged_RuggednessFinal grid_longitude grid_latitude D_Coast_km landsuitability  
sum CHIRPS_Prec_mm  mt_binary_mean grid_perc_h20 elevation Rugged_RuggednessFinal grid_longitude grid_latitude D_Coast_km landsuitability  if Centralized_8hr == 1

gen Counterfactual_55 = 1 
replace Counterfactual_55 = 0 if elevation <  1.595828
replace Counterfactual_55 = 0 if Rugged_RuggednessFinal > 10.58058 & CHIRPS_Prec_mm != .
replace Counterfactual_55 = 0 if Rugged_RuggednessFinal < .0163909
replace Counterfactual_55 = 0 if grid_latitude <   -30.45456
replace Counterfactual_55 = 0 if grid_latitude > 19.54544 & CHIRPS_Prec_mm != .
replace Counterfactual_55 = 0 if CHIRPS_Prec_mm >  3417.807 & CHIRPS_Prec_mm != .
replace Counterfactual_55 = 0 if CHIRPS_Prec_mm <  8.726335
replace Counterfactual_55 = 0 if D_Coast_km <  .0791719
replace Counterfactual_55 = 0 if landsuitability <   .001389  

save "Appendix/Larger Grids/FINAL_CLIPPED_55degrees.dta", replace
}
